package View;


import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import IDbConnections.TargetDatabase;

@SuppressWarnings("serial")
public class MyGeneratedCode extends JFrame{

	private JTextArea mycode;
	private String theCode;
	private JButton selecteerAlles;
	private JButton applyBR;
	private String errorMSG;
	
	public MyGeneratedCode(String code){
		theCode = code;
		setLayout(new FlowLayout());
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		setSize(500, 500);
		int screenHeight = screenSize.height;
		int screenWidth = screenSize.width;
		setLocation((screenWidth / 2) - (getWidth() / 2), (screenHeight / 2) - (getHeight() / 2));
		setVisible(true);
		mycode = new JTextArea(theCode);
		mycode.setLineWrap(true);
		JScrollPane pScroll = new JScrollPane(mycode, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		//mycode.setPreferredSize(new Dimension(470,400));
		pScroll.setPreferredSize(new Dimension(480,400));
		add(pScroll);
		
		JPanel knoppenPanel = new JPanel();
			selecteerAlles = new JButton("Copy to clipboard");
			applyBR = new JButton("Apply on Target Database");
			
			selecteerAlles.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent arg0) {
					mycode.selectAll();
					mycode.copy();
					selecteerAlles.transferFocusBackward();
				}
				
			});
			
			applyBR.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent arg0) {
					TargetDatabase targetDB = new TargetDatabase();
					try {
					targetDB.applyBusinessRules(theCode);
					}
					catch(SQLException ex) {
						errorMSG = "Error while applying Business Rule(s):\n" + ex.getMessage();
						JFrame frame = new JFrame();
						JOptionPane.showMessageDialog(frame, errorMSG, "Business rule database error!", JOptionPane.ERROR_MESSAGE);
					}
					finally {
						if (errorMSG == null) {
							JFrame frame = new JFrame();
							JOptionPane.showMessageDialog(frame, "The selected business rule(s), are succesfully added to the target database.");
						}
					}
				}
			});
			
		knoppenPanel.add(selecteerAlles);
		knoppenPanel.add(applyBR);
		add(knoppenPanel);
	}
	
}
